var $btner = $(".p2 .nextpt .btn"),
	roundcent2 = new Array(0,0,0),
	roundcent = "",
	roundwith = new Array(0,0,0),
	interval,
	timerprev,
	sum = 0,
	rounder = 0,
	ruler = 0,
	finalnum,
	nameslist2 = new Array("","",""),
	nameslist = "",
	d1,
	d2;
$btner.on("touchstart",function(){
	$(this).addClass("btnbigger");
	$(".food").fadeOut(300).next().fadeIn(300);
	/*interval = setInterval(function(){		
		sum += 17;
	},17);*/
	d1 = new Date();
});
$btner.on("touchend",function(){
	$(this).removeClass("btnbigger");
	clearInterval(interval);
    /*interval = null;*/
   	d2 = new Date();
   	sum = d2 - d1;
    roundcent2[rounder] = sum;
    doResult();
});

function getArrayItems(arr, num){
    var temp_array = new Array();
    for (var e=0;e<arr.length;e++) {
        temp_array.push(arr[e]);
    }
    var return_array = new Array();
    for (var i = 0; i<num; i++) {
        if (temp_array.length>0) {
            var arrIndex = Math.floor(Math.random()*temp_array.length);
            return_array[i] = temp_array[arrIndex];
            temp_array.splice(arrIndex, 1);
        } else {
            break;
        }
    }
    return return_array;
}
var arraytime = new Array(1,2,3,4,5,6,7,8,9,10);
var arraytime2 = getArrayItems(arraytime,3);
function checkEach(num){
	switch(num){
		case 1:
			$(".p2 .prevpt .p2-slogan").attr("src","img/t01.png");
			$(".p2 .prevpt .p2-num").html("10");
			$(".p2 .nextpt .food").attr("src","img/c01.png");
			$(".pop .foodbox .img1").attr("src","img/z1.1.png").next().attr("src","img/z1.2.png")
			.next().attr("src","img/z1.3.png");
			ruler = 10000;
			nameslist2[rounder] = "十秒涮九节虾";
			break;
		case 2:
			$(".p2 .prevpt .p2-slogan").attr("src","img/t02.png");
			$(".p2 .prevpt .p2-num").html("8");
			$(".p2 .nextpt .food").attr("src","img/c02.png");
			$(".pop .foodbox .img1").attr("src","img/z2.1.png").next().attr("src","img/z2.2.png")
			.next().attr("src","img/z2.3.png");
			ruler = 8000;
			nameslist2[rounder] = "八秒涮水晶饺";
			break;
		case 3:
			$(".p2 .prevpt .p2-slogan").attr("src","img/t03.png");
			$(".p2 .prevpt .p2-num").html("9");
			$(".p2 .nextpt .food").attr("src","img/c03.png");
			$(".pop .foodbox .img1").attr("src","img/z3.1.png").next().attr("src","img/z3.2.png")
			.next().attr("src","img/z3.3.png");
			ruler = 9000;
			nameslist2[rounder] = "九秒涮肥牛";
			break;
		case 4:
			$(".p2 .prevpt .p2-slogan").attr("src","img/t04.png");
			$(".p2 .prevpt .p2-num").html("5");
			$(".p2 .nextpt .food").attr("src","img/c04.png");
			$(".pop .foodbox .img1").attr("src","img/z4.1.png").next().attr("src","img/z4.2.png")
			.next().attr("src","img/z4.3.png");
			ruler = 5000;
			nameslist2[rounder] = "五秒涮豆腐";
			break;
		case 5:
			$(".p2 .prevpt .p2-slogan").attr("src","img/t05.png");
			$(".p2 .prevpt .p2-num").html("4");
			$(".p2 .nextpt .food").attr("src","img/c05.png");
			$(".pop .foodbox .img1").attr("src","img/z5.1.png").next().attr("src","img/z5.2.png")
			.next().attr("src","img/z5.3.png");
			ruler = 4000;
			nameslist2[rounder] = "四秒涮蟹肉棒";
			break;
		case 6:
			$(".p2 .prevpt .p2-slogan").attr("src","img/t06.png");
			$(".p2 .prevpt .p2-num").html("7");
			$(".p2 .nextpt .food").attr("src","img/c06.png");
			$(".pop .foodbox .img1").attr("src","img/z6.1.png").next().attr("src","img/z6.2.png")
			.next().attr("src","img/z6.3.png");
			ruler = 7000;
			nameslist2[rounder] = "七秒涮萝卜";
			break;
		case 7:
			$(".p2 .prevpt .p2-slogan").attr("src","img/t07.png");
			$(".p2 .prevpt .p2-num").html("2");
			$(".p2 .nextpt .food").attr("src","img/c07.png");
			$(".pop .foodbox .img1").attr("src","img/z7.1.png").next().attr("src","img/z7.2.png")
			.next().attr("src","img/z7.3.png");
			ruler = 2000;
			nameslist2[rounder] = "二秒涮青菜";
			break;
		case 8:
			$(".p2 .prevpt .p2-slogan").attr("src","img/t08.png");
			$(".p2 .prevpt .p2-num").html("1");
			$(".p2 .nextpt .food").attr("src","img/c08.png");
			$(".pop .foodbox .img1").attr("src","img/z8.1.png").next().attr("src","img/z8.2.png")
			.next().attr("src","img/z8.3.png");
			ruler = 1000;
			nameslist2[rounder] = "一秒涮白菜";
			break;
		case 9:
			$(".p2 .prevpt .p2-slogan").attr("src","img/t09.png");
			$(".p2 .prevpt .p2-num").html("6");
			$(".p2 .nextpt .food").attr("src","img/c09.png");
			$(".pop .foodbox .img1").attr("src","img/z9.1.png").next().attr("src","img/z9.2.png")
			.next().attr("src","img/z9.3.png");
			ruler = 6000;
			nameslist2[rounder] = "六秒涮土豆";
			break;
		case 10:
			$(".p2 .prevpt .p2-slogan").attr("src","img/t10.png");
			$(".p2 .prevpt .p2-num").html("3");
			$(".p2 .nextpt .food").attr("src","img/c10.png");
			$(".pop .foodbox .img1").attr("src","img/z10.1.png").next().attr("src","img/z10.2.png")
			.next().attr("src","img/z10.3.png");
			ruler = 3000;
			nameslist2[rounder] = "三秒涮金针菇";
			break;
	}
}
checkEach(arraytime2[rounder]);

function doResult(){
	roundwith[rounder] = roundcent2[rounder]/ruler;
	var thenum =  roundcent2[rounder]/1000;
	thenum = thenum.toFixed(3);
	$(".timebox .timenum").html(thenum);
	if(roundwith[rounder]<=0.299){
		$(".window .txt").attr("src","img/P5_png06.png");
	}else if(roundwith[rounder]>=0.3&&roundwith[rounder]<=0.599){
		$(".window .txt").attr("src","img/P5_png07.png");
	}else if(roundwith[rounder]>=0.6&&roundwith[rounder]<=0.899){
		$(".window .txt").attr("src","img/P5_png08.png");
	}else if(roundwith[rounder]>=0.9&&roundwith[rounder]<=0.995){
		$(".window .txt").attr("src","img/P5_png09.png");
	}else if(roundwith[rounder]>=0.996&&roundwith[rounder]<=1.004){
		$(".window .txt").attr("src","img/P5_png10.png");
	}else if(roundwith[rounder]>=1.005&&roundwith[rounder]<=1.1){
		$(".window .txt").attr("src","img/P5_png11.png");
	}else if(roundwith[rounder]>=1.101&&roundwith[rounder]<=1.3){
		$(".window .txt").attr("src","img/P5_png12.png");
	}else if(roundwith[rounder]>=1.301&&roundwith[rounder]<=3){
		$(".window .txt").attr("src","img/P5_png13.png");
	}else if(roundwith[rounder]>3){
		$(".window .txt").attr("src","img/P5_png14.png");
	}	
	if(roundwith[rounder]<0.996){
		$(".foodbox .img1").removeClass("none").next().addClass("none").next().addClass("none");
	}if(roundwith[rounder]>=0.996&&roundwith[rounder]<=1.004){
		$(".foodbox .img1").addClass("none").next().removeClass("none").next().addClass("none");
	}if(roundwith[rounder]>1.004){
		$(".foodbox .img1").addClass("none").next().addClass("none").next().removeClass("none");
	}
	roundwith[rounder] = Math.abs(roundcent2[rounder]-ruler)/ruler;
	if(rounder < 2){
		rounder = rounder + 1;
		if(rounder==1){
			$(".p2-turn").attr("src","img/P2_png02.png");
		}else if(rounder==2){
			$(".p2-turn").attr("src","img/P2_png03.png");
		}	
	}else{
		$(".window .nextbtn").hide().next().removeClass("none");
		finalnum = roundwith[0]/3 + roundwith[1]/3 + roundwith[2]/3;
		finalnum = finalnum.toFixed(3);
		$(".f1 .txtbox .timenum").html(finalnum);
		$(".f2 .txtbox .timenum").html(finalnum);
	}
	var timer = setTimeout(function(){
		sum = 0;
		$(".pop").fadeIn(600);
	},100);
}

$(".nextbtn").on("touchstart",function(){
	$(".pop").hide();
	$(".nextpt").hide();
	$(".food").show().next().hide();
	checkEach(arraytime2[rounder]);
	$(".prevpt").show();	
	timerprev = setTimeout(function(){
		$(".p2").find(".prevpt").hide().next().show();
		clearTimeout(timerprev);
		timerprev = null;
	},3000);
});
$(".seebtn").on("touchstart",function(){
	
	nameslist = $.each(nameslist2,function(i,e){
		
		//console.log(nameslist);
	});
	roundcent = $.each(roundcent2,function(i,e){
		e = e/1000 + "秒";
		roundcent = roundcent+e+";";
		nameslist = nameslist+nameslist2[i]+";";
		if(i == 2){
			//console.log(roundcent);
			if(finalnum>=0.5){
				//f3
				$(".p2").hide().next().show().find(".f3").show();		
			}else{
				//f2
				window.Interface.challengeResults({
					starType: "game",
					score: finalnum,
					question: nameslist,
					answer: roundcent,
				},function(res){
					showGameResults(res.type,res.ranking,res.isSend);
				});
			}
		}
	});
	
	//alert(nameslist+","+roundcent);	
}); 

function showGameResults(ending,rank,iSend){
	//ending = "challenger";
	//iSend = true;
	if(!iSend){
		$(".f1 .bac").addClass("bac2").next().addClass("bei2").next().addClass("none");
		$(".f2 .star").addClass("star2").next().addClass("none");
	}
	if (ending == "challenger") {
		//no.1
		$(".p2").hide().next().show().find(".f1").show();
	}else{
		//过关
		$(".f2").find(".timenum2").html(rank);//名次
		$(".p2").hide().next().show().find(".f2").show();
	}
	
}